package com.jhgsys.internal.peccancy.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.peccancy.entity.PeccancyClauseRuleRecord;
import com.jhgsys.internal.peccancy.service.IPeccancyClauseRuleRecordService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Date;

/**
 * 奖惩规则变更记录 Controller
 *
 * @author admin
 * @date 2024-08-08 13:20:30
 */
@Slf4j
@Validated
@Controller
public class PeccancyClauseRuleRecordController extends BaseController {

    @Autowired
    private IPeccancyClauseRuleRecordService peccancyClauseRuleRecordService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "peccancyClauseRuleRecord")
    @RequiresPermissions("peccancyClauseRuleRecord:view")
    public String peccancyClauseRuleRecordIndex(){
        return jumpPage("peccancy/peccancyClauseRuleRecord/peccancyClauseRuleRecordList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "peccancyClauseRuleRecord/add")
    @RequiresPermissions("peccancyClauseRuleRecord:add")
    public String peccancyClauseRuleRecordAdd(){
        return jumpPage("peccancy/peccancyClauseRuleRecord/peccancyClauseRuleRecordEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "peccancyClauseRuleRecord/update/{id}")
    @RequiresPermissions("peccancyClauseRuleRecord:edit")
    public String peccancyClauseRuleRecordUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("peccancyClauseRuleRecord", peccancyClauseRuleRecordService.findPeccancyClauseRuleRecordById(id));
        return jumpPage("peccancy/peccancyClauseRuleRecord/peccancyClauseRuleRecordEdit");
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-08-08 13:20:30
     */
    @GetMapping("peccancyClauseRuleRecord")
    @ResponseBody
    @RequiresPermissions("peccancyClauseRuleRecord:view")
    public FebsResponse getAllPeccancyClauseRuleRecords(PeccancyClauseRuleRecord peccancyClauseRuleRecord) {
        return new FebsResponse().success().data(peccancyClauseRuleRecordService.findPeccancyClauseRuleRecords(peccancyClauseRuleRecord));
    }

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-08-08 13:20:30
     */
    @GetMapping("peccancyClauseRuleRecord/list")
    @ResponseBody
    @RequiresPermissions("peccancyClauseRuleRecord:view")
    public FebsResponse peccancyClauseRuleRecordList(QueryRequest request, PeccancyClauseRuleRecord peccancyClauseRuleRecord) {
        Map<String, Object> dataTable = getDataTable(this.peccancyClauseRuleRecordService.findPeccancyClauseRuleRecordPage(request, peccancyClauseRuleRecord));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 PeccancyClauseRuleRecord
     * @author admin
     * @date 2024-08-08 13:20:30
     */
    @Log("新增PeccancyClauseRuleRecord")
    @PostMapping("peccancyClauseRuleRecord")
    @ResponseBody
    @RequiresPermissions("peccancyClauseRuleRecord:add")
    public FebsResponse addPeccancyClauseRuleRecord(@Valid PeccancyClauseRuleRecord peccancyClauseRuleRecord) throws FebsException {
        try {
            peccancyClauseRuleRecord.setCreatedBy(getCurrentUser().getUserName());
            this.peccancyClauseRuleRecordService.createPeccancyClauseRuleRecord(peccancyClauseRuleRecord);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增PeccancyClauseRuleRecord失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 逻辑删除 PeccancyClauseRuleRecord
     * @author admin
     * @date 2024-08-08 13:20:30
     */
    @Log("逻辑删除PeccancyClauseRuleRecord")
    @GetMapping("peccancyClauseRuleRecord/delete/{id}")
    @ResponseBody
    @RequiresPermissions("peccancyClauseRuleRecord:delete")
    public FebsResponse deletePeccancyClauseRuleRecord(@PathVariable("id") String peccancyClauseRuleRecordIds) throws FebsException {
        try {
            PeccancyClauseRuleRecord peccancyClauseRuleRecord = new PeccancyClauseRuleRecord();
            peccancyClauseRuleRecord.setDeletedBy(getCurrentUser().getUserName());
            peccancyClauseRuleRecord.setDeletedTime(new Date());
            peccancyClauseRuleRecord.setDeletedFlg(FebsConstant.INVALID);
            this.peccancyClauseRuleRecordService.deletePeccancyClauseRuleRecord(peccancyClauseRuleRecord,peccancyClauseRuleRecordIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除PeccancyClauseRuleRecord失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
    * 物理删除 PeccancyClauseRuleRecord
    * @author admin
    * @date 2024-08-08 13:20:30
    */
    @Log("物理删除PeccancyClauseRuleRecord")
    @GetMapping("peccancyClauseRuleRecord/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("peccancyClauseRuleRecord:delete")
    public FebsResponse physicallyDeletePeccancyClauseRuleRecord(@PathVariable("id") String peccancyClauseRuleRecordIds) throws FebsException {
        try {
            this.peccancyClauseRuleRecordService.physicallyDeletePeccancyClauseRuleRecord(peccancyClauseRuleRecordIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除PeccancyClauseRuleRecord失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 修改 PeccancyClauseRuleRecord
     * @author admin
     * @date 2024-08-08 13:20:30
     */
    @Log("修改PeccancyClauseRuleRecord")
    @PostMapping("peccancyClauseRuleRecord/update")
    @ResponseBody
    @RequiresPermissions("peccancyClauseRuleRecord:edit")
    public FebsResponse updatePeccancyClauseRuleRecord(PeccancyClauseRuleRecord peccancyClauseRuleRecord) throws FebsException {
        try {
            peccancyClauseRuleRecord.setUpdatedBy(getCurrentUser().getUserName());
            this.peccancyClauseRuleRecordService.updatePeccancyClauseRuleRecord(peccancyClauseRuleRecord);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改PeccancyClauseRuleRecord失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 导出 PeccancyClauseRuleRecord的excel
     * @author admin
     * @date 2024-08-08 13:20:30
     */
    @PostMapping("peccancyClauseRuleRecord/excel")
    @ResponseBody
    @RequiresPermissions("peccancyClauseRuleRecord:view")
    public void export(QueryRequest queryRequest, PeccancyClauseRuleRecord peccancyClauseRuleRecord, HttpServletResponse response) throws FebsException {
        try {
            List<PeccancyClauseRuleRecord> peccancyClauseRuleRecords = this.peccancyClauseRuleRecordService.findPeccancyClauseRuleRecordPage(queryRequest, peccancyClauseRuleRecord).getRecords();
            ExcelKit.$Export(PeccancyClauseRuleRecord.class, response).downXlsx(peccancyClauseRuleRecords, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
